package core;
import repository.Repository;
import java.io.*;
import java.util.Arrays;
import core.JitBranch;

public class branchCheckOut {
    public static void checkout(String branchName) throws IOException {
        File headPath = new File(Repository.getGitDir() + File.separator + "HEAD");
        String branchname = "ref: refs/heads/"+branchName+"\n";
        String filePath = Repository.getGitDir()+File.separator+"refs"+File.separator+"heads";
        String[] ListOfBranch = new File(filePath).list();
        if(Arrays.asList(ListOfBranch).contains(branchName)){
            try {
                FileWriter fw = new FileWriter(headPath, false);
                fw.write(branchname);
                fw.flush();
                fw.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("branch成功检出");
        }
        else {
            throw new IOException("分支不存在！");
        }


    }

    public static String FileToread(File path) throws IOException{
        InputStream inpS = new FileInputStream(path);
        byte[] bytes = new byte[1024];
        int lenOfFile = inpS.read(bytes);
        String stringl = null;
        while (lenOfFile!=-1){
            stringl = new String(bytes,0,lenOfFile);
            lenOfFile = inpS.read(bytes);
        }
        inpS.close();
        return stringl;
    }
}